﻿Imports System.Data.OleDb

Public Class Battler
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' initialise variable on page load
        lbl.Text = ""

    End Sub

    Protected Sub bt_Select1_Click(sender As Object, e As EventArgs) Handles bt_Select1.Click
        ' variable dec
        Randomize()
        Dim avalue As Integer = CInt(Int((100 * Rnd()) + 1))
        Dim bvalue As Integer = CInt(Int((100 * Rnd()) + 1))
        Dim Winner As Integer
        Dim Loser As Integer

        ' randomising result to work out the winner and updating pic
        If avalue > bvalue Then
            Winner = dl_1.SelectedIndex + 1 'zero based
            Dim Val3 = dl_1.SelectedValue ' winner
            Image3.ImageUrl = Val3
            Loser = dl_2.SelectedIndex + 1 ' loser

        Else

            Winner = dl_2.SelectedIndex + 1 ' zero based
            Dim Val4 As String
            Val4 = dl_2.SelectedValue ' winner
            Image3.ImageUrl = Val4
            Loser = dl_1.SelectedIndex + 1 ' loser
        End If



        'Open Database and update values for winners and losers

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("roboticsConnectionString").ConnectionString)
        Dim SqlString As String = "update mastTable1 set Wins = 1000 where ID = (Winner)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = System.Data.CommandType.Text
        ' updating winners
        cmd.CommandText = "UPDATE mastTable1 SET Wins = Wins + @f1, Ranking = Ranking + @f2 WHERE ID = @f3"
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", 1)
        cmd.Parameters.AddWithValue("@f2", 100)
        cmd.Parameters.AddWithValue("@f3", Winner)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        ' updating losers
        cmd.CommandText = "UPDATE mastTable1 SET Losses = Losses + @f1, Ranking = Ranking + @f2 WHERE ID = @f3"
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", 1)
        cmd.Parameters.AddWithValue("@f2", 0)
        cmd.Parameters.AddWithValue("@f3", Loser)
        cmd.ExecuteNonQuery()

        oleDbConn.Close()

        lbl.Text = "The Winner!!!"


    End Sub

    ' updating individual images
    Protected Sub dl_1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles dl_1.SelectedIndexChanged
        Dim Val1 As String
        Val1 = dl_1.SelectedValue
        Image1.ImageUrl = Val1

    End Sub

    ' updating individual images
    Protected Sub dl_2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles dl_2.SelectedIndexChanged
        Dim Val2 As String
        Val2 = dl_2.SelectedValue
        Image2.ImageUrl = Val2
    End Sub

    



End Class